package com.xs.bookspoststation.db;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.xs.bookspoststation.bean.Student;

import java.util.ArrayList;
import java.util.List;

public class StudentBean {
    private Context context;
    private DBHelperStudent dbHelperStudent;
    public StudentBean(Context context){
        this.context=context;
        dbHelperStudent = new DBHelperStudent(context,"student.db",null,1);
    }

    @SuppressLint("Range")
    public List<Student> findAll(){
        List<Student> students = null;
        SQLiteDatabase db = dbHelperStudent.getWritableDatabase();
        if (db.isOpen()) {
            Cursor cursor = db.query("studentInfo", null, null, null, null, null, null);
            students = new ArrayList<Student>();
            while (cursor.moveToNext()) {
                Student student = new Student();
                //Xxx getXxx(columnIndex):根据字段下标得到对应的值
                //int getColumnIndex():根据字段名得到对应的下标
                //cursor.getLong()：以 long 形式返回所请求列的值。
                //getColumnIndex() 获取给定列名的从零开始的列索引,如果列名不存在返回-1
                //sno 学号 sname 姓名 spwd 密码 sex 性别 sphone 电话 sclass 班级 spro 专业 sde 系部
                String sno = cursor.getString(cursor.getColumnIndex("sno"));
                student.setSno(sno);
                String sname = cursor.getString(cursor.getColumnIndex("sname"));
                student.setSname(sname);
                String spwd = cursor.getString(cursor.getColumnIndex("spwd"));
                student.setSpwd(spwd);
                String sex = cursor.getString(cursor.getColumnIndex("sex"));
                student.setSex(sex);
                String sphone = cursor.getString(cursor.getColumnIndex("sphone"));
                student.setSphone(sphone);
                String sclass = cursor.getString(cursor.getColumnIndex("sclass"));
                student.setSclass(sclass);
                String spro = cursor.getString(cursor.getColumnIndex("spro"));
                student.setSpro(spro);
                String sde = cursor.getString(cursor.getColumnIndex("sde"));
                student.setSde(sde);
                students.add(student);
            }
            cursor.close();
            db.close();
        }
        return students;
    }
}
